#!/usr/bin/env python

import sys, os, locale
from course_class import Course
from os3.types.money import Money

def init ( l, admin = 0 ):
	from liwe import parts

	parts.add ( l.layman, [ "ext" ] )
	parts.add_wwl ( l.layman, 'popbox' )

	l.layman.add_js ( "modules/course/js/course.js" )
	l.layman.add_css ( "modules/course/gfx/course.css" )

	l.layman.add_js_code ( "course.init();" )

	if admin and int ( admin ):
		l.layman.add_js ( "modules/course/js/course.admin.js" )
		l.layman.add_js ( "os3wwl/checkbox/checkbox.js" )
		l.layman.add_js ( "os3wwl/checkbox/form.checkbox.js" )
		l.layman.add_css ( "modules/course/gfx/course.admin.css" )
		l.layman.add_css ( "os3wwl/checkbox/gfx/checkbox.css" )
		l.layman.add_js_code ( "course.admin.init();" )

	return ''


def show ( l ): 


	id_course = l.uri_data.get ( "id_course", "0" )
	how_many = l.uri_data.get ( "how_many", "0" )

	c =  Course ( l )
	course = c.get_course ( id_course = id_course )

	course [ '_CALENDAR' ] = _course_calendar ( l, id_course, how_many )
		
	txt = c.templates [ 'SHOW-LAYOUT' ] % course

	return { "_txt" : txt }


def _course_calendar ( l, id_course, how_many ):

	#FIXME:Implementarea livello di LiWe
	#locale.setlocale ( locale.LC_ALL, "it_IT.utf8" )

	c =  Course ( l )
	calendar = c.list_calendar ( start_point = 0, quant = how_many, id_course = id_course , mode = 'valid' )


	if not calendar.get( "rows", [] ) : return c.templates [ 'CALENDAR-LIST-NO-ROWS' ];

	_html = c.templates [ 'CALENDAR-LIST-TB-START' ];
	_html += c.templates [ 'CALENDAR-LIST-HEADER'];
	_html += c.templates [ 'CALENDAR-LIST-TB-END' ];
	odd_even = "odd"
	for row in  calendar.get( "rows", [] ):
		row [ '_start_date' ] = row [ 'start_date' ].strftime ( "%A %d %B %Y" )
		row [ "_tutor" ] = row [ 'trainer_login' ] ;
		row [ '_HREF_REGISTER' ] =  c.templates [ '_HREF_REGISTER' ] % row
		row [ '_ACTION_REGISTER'] = c.templates [ '_ACTION_REGISTER' ] % row
		row [ '_register' ] = c.templates [ 'REGISTER-LNK' ] %  row

		_fix_money ( row )
				
		dct_price = { '_price_vat' : row [ 'price_vat' ], '_max_value' : row [ 'max_end_value' ] }
		row [ '_price' ] = _fix_tpl ( c.templates [ 'CALENDAR-DISCOUNT' ] ) % dct_price
		
		 

		if row [ 'max_end_value' ] == row [ 'price_vat' ] :
			row [ '_price' ] = _fix_tpl ( c.templates [ 'CALENDAR-END-VALUE' ] )  % dct_price

		if row [ 'price_vat' ] == 0:
			row [ '_price' ] = c.templates [ 'CALENDAR-GRATIS' ]

		odd_even = "odd" if odd_even == "even" else "even"

		row [ 'odd_even' ] = odd_even
		
		_html += c.templates [ 'CALENDAR-LIST-ROW' ] % row;

	_html += c.templates [ 'CALENDAR-LIST-TB-END' ]
	_html += c.templates [ 'CALENDAR-LIST-FOOTER' ]

	return _html

def _fix_tpl ( tpl ):
    return tpl.replace ( ")m", ")s" )	
	
def _fix_money ( data ):
    _money = Money ()
    _money.dsep = ","
    _money.tsep = "."

    #prices = [ 'final_net', 'final_vat', 'price_net', 'price_vat', '_vat' ]
    prices = [ 'price_vat', 'max_end_value' ]
    for price in prices:
        m = _money.from_long_int ( data.get ( price, 0 ) ) 
        data [ price ] = str ( m )

